package main

import (
	"context"
	"fmt"
	"sync"
	"time"

	"github.com/louis-xie-programmer/go-local-cache/cache"
)

func main() {
	c := cache.NewCache(cache.CacheOptions{
		DefaultTTL:    5 * time.Second,
		EmptyTTL:      1 * time.Second,
		RefreshFactor: 0.3,
		ShardCount:    8,
		TotalMaxBytes: 1 << 20,
	})

	loader := func(ctx context.Context) (interface{}, time.Duration, error) {
		time.Sleep(10 * time.Millisecond)
		return fmt.Sprintf("val-%d", time.Now().UnixNano()), 5 * time.Second, nil
	}

	var wg sync.WaitGroup
	numGoroutines := 50
	numOps := 200

	start := time.Now()
	for i := 0; i < numGoroutines; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for j := 0; j < numOps; j++ {
				key := fmt.Sprintf("key-%d", j%100)
				c.GetOrLoadCtx(context.Background(), key, loader)
			}
		}()
	}

	wg.Wait()
	fmt.Println("Operations completed in:", time.Since(start))
	fmt.Println("Shard[0] items:", c.ShardItems(0))
	fmt.Println("Shard count:", c.ShardCount())
}
